<template>
  <div class="login">
    <div class="login_box">
      <!-- 头像区域结构 -->
      <div class="avatar">
        <img src="../assets/pho.jpg"/>
      </div>
      <!-- input结构 -->
      <el-form :model="loginForm" :rules="testForm" class="fm" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input  placeholder="账号" v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input placeholder="密码" v-model="loginForm.password" prefix-icon="iconfont icon-lock_fill" type="password"></el-input>
        </el-form-item>
        <el-form-item class="fm_btn">
          <el-button type="primary" @click="getData">登录</el-button>
          <el-button type="info" @click="resetData">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单data
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 校验表单
      testForm: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 提交获得表单数据
    getData () {
      this.$refs.loginFormRef.validate(async flag => {
        if (!flag) return
        const { data: res } = await this.$axios.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    // 重置表单内容
    resetData (loginForm) {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped lang='less'>
  .login {
    height: 100%;
    background: linear-gradient(270deg,rgb(127, 144, 236), rgb(212, 103, 192));

    .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform:translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;

      // 头像区域样式
      .avatar {
        position: absolute;
        left: 50%;
        top: 0;
        transform:translate(-50%,-50%);
        width: 120px;
        height: 120px;
        border-radius: 50%;
        padding: 5px;
        background-color: #fff;

          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
      }

      // 表单区域样式
      .fm {
        padding: 15px;
        margin-top: 90px;

        .fm_btn {
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
</style>
